<?php

class MathFuncs
{
   public static function makeArray() {
      $numArgs = func_num_args();
      for ($i = 0; $i < $numArgs; $i++) {
         $val = func_get_arg($i);
	 $result[$i] = $val;
      }
      return $result;
   }

   public static function makeSet() {
      $numArgs = func_num_args();
      for ($i = 0; $i < $numArgs; $i++) {
         $val = func_get_arg($i);
      	 $result[$val] = true;
      }
      return $result;
   }

   public static function double($val) {
      return $val * 2;
   }

   public static function equals($left, $right) {
      return $left == $right;
   }

   public static function equalFractions($user, $soln) {
      $arr = array();
      $tok = strtok($user, " /");
      while ($tok !== false) {
         $arr[] = $tok;
	 $tok = strtok(" /");
      }
      $numTokens = count($arr);

      if ($numTokens == 3) {
         $numerator = $arr[0] * $arr[2] + $arr[1];
	 $denominator = $arr[2];
	 $formatted = $numerator."/".$denominator;
      } else if ($numTokens == 2) {
         $numerator = $arr[0];
	 $denominator =  $arr[1];
	 $formatted = $numerator."/".$denominator;
      } else if ($numTokens == 1) {
         $formatted = $arr[0];
      } else 
         return false;

      return $formatted == $soln;
   }

   public static function random($low, $high, $exclude = null) {
      $val = rand($low, $high);
      if ($exclude == null)
         return $val;
      while (array_key_exists($val, $exclude)) {
         $val = rand($low, $high);
      }
      return $val;
   }

   public static function cleanUp($str) {
      $str = preg_replace("/\+ -/", "- ", $str);
      $str = preg_replace("/1([a-z])/", "$1", $str);
      $str = preg_replace("/0([a-z])/", "0", $str);
      return $str;
   }

   public static function lcm($numerator, $denominator) {
      if ($denominator == 1) return 1;
      $end = $numerator > $denominator? $numerator : $denominator;
      $lcm = 1;
      for ($i = 2; $i <= $end; $i++) {
         if ($numerator % $i == 0 && $denominator % $i == 0)
	    $lcm = $i;
      }
      return $lcm;
   }

   public static function reduce($numerator, $denominator) {
      $lcm = MathFuncs::lcm($numerator, $denominator);
      $denominator /= $lcm;
      $numerator /= $lcm;
      if ($denominator != 1)
         return "$numerator/$denominator";
      else
         return $numerator;
   }

   public static function factor($val) {
      if ($val < 2) 
         return false;

      $temp = rand(2, $val);
      while ($val % $temp != 0) {
         $temp++;
      }
      return $temp;
   }

   public static function lcd($one, $two) {
      if ($one % $two == 0)
      	 return $one;

      if ($two % $one == 0)
      	 return $two;

      return ($one * $two);
   }

}

?>